layui.use(['upload','element','table','form','layer','laytpl'],function(){
	var $ = layui.jquery
  		,upload = layui.upload
			,table = layui.table
			,layer = layui.layer
			,element =layui.element
			,laytpl = layui.laytpl
			,form = layui.form;
	
	//1.查询教师信息 
	
	
	var teacherList= table.render({
		elem: '#teacherList',
		url: "http://localhost:8888/teacher/getTeacherList",
		cols: [
			[{
				field: 'id',
				width: 80,
				title: 'ID',
				sort: true
			}, {
				field: 'qq',
				title: 'qq号'
			}, {
				field: 'name',
				title: '名字',
				sort: true
			}, {
				field: 'freeTime',
				title: '可预约时间',
				sort: true,
				templet:function(d){
					var arr = d.freeTime.split(",")
					var html ="";
					
					for(var i = 0;i<arr.length;i++){
						if(arr[i]==2){
							html += '<span class="layui-badge layui-bg-blue">周一</span>&nbsp;'
						} else if(arr[i]==3){
							html += '<span class="layui-badge layui-bg-blue">周二</span>&nbsp;'
						}else if(arr[i]==4){
							html += '<span class="layui-badge layui-bg-blue">周三</span>&nbsp;'
						}else if(arr[i]==5){
							html += '<span class="layui-badge layui-bg-blue">周四</span>&nbsp;'
						}else if(arr[i]==6){
							html += '<span class="layui-badge layui-bg-blue">周五</span>&nbsp;'
						}else if(arr[i]==7){
							html += '<span class="layui-badge layui-bg-orange">周六</span>&nbsp;'
						}else if(arr[i]==1){
							html += '<span class="layui-badge layui-bg-orange">周日</span>&nbsp;'
						}					
					}
					return html
				}
			},{
				fixed: 'right',
				width: 180,
				align: 'center',
				title: "操作",
				toolbar: '#bar'
			}]
		]
	});
	
	var arr = new Array();
	form.on('checkbox(test1)', function(data) {
		if(data.elem.checked == true){
//			console.log(arr.toString())
			arr.push(data.value)		
			for(var i = 0; arr.length - 1 > i; i++) {
				for(var j = i + 1; j < arr.length; j++) {
					if(arr[j] == arr[i]) {			
						arr.splice(j, 1); //删除之后，数组长度随之减少 
						j--;
					}
				}
			}
			arr.join(",")
		} else {
//			console.log(data.value +"没有选择")
			for(var i = 0;i<arr.length;i++){
				if(arr[i]==data.value){
					arr.splice(i,1);
				}
			}
		}				
		
		for(var i = 0; arr.length - 1 > i; i++) {
			for(var j = i + 1; j < arr.length; j++) {
				if(arr[j] == arr[i]) {				
					arr.splice(j, 1); //删除之后，数组长度随之减少 
					j--;
				}
			}
		}		
//		console.log(arr.toString())
		$("#freeTime").val(arr.toString())
	});
	//监听提交
	form.on('submit(submitTeacher)', function(data) {
//		console.log(data.field)
		
		$.ajax({
			type: "post",
			url: "http://localhost:8888/teacher/register",
			data: data.field,
			success: function(data) {
				layer.open({
					content: '添加成功,请查看',
					yes: function(index, layero) {
						table.reload()
						//do something
						 layer.close(index); //如果设定了yes回调，需进行手工关闭
					}
				});
			},
			error: function() {
				layer.msg('修改失败', {
					anim: 6
				});
			}
		});
	
		return false;
	});
	
	table.on('tool(teacherList)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data; //获得当前行数据
		var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		var tr = obj.tr; //获得当前行 tr 的DOM对象
		//console.log("ok")
		if(layEvent === 'detail') { //查看
			//do somehing
		} else if(layEvent === 'del') { //删除
			layer.confirm('真的删除行么', function(index) {
				obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
				//				      console.log(data.id)
				layer.close(index);
				//				      向服务端发送删除指令
				$.ajax({
					url: "http://localhost:8888/teacher/delete?id=" + data.id,
					type: "get",
					success: function(data) {
						layer.msg("删除成功")
						location.reload()
					}
				})
			});
		} else if(layEvent === 'edit') { //编辑
			//do something
			//window.oepn('editPaper.html?paperId=' + data.id)
			//同步更新缓存对应的值
		}
	});
});
